/**
 * 行政老师导入岗位
 * 
 * /xingzheng/company/add
 */


const Year = require('../../../models/years')
const Company = require('../../../models/companys')

const utils = require('../../../utils/index')
const $ = require('../../../utils/common')

const add_jobs = async function (req, res) {
    try {
        // 检查参数，学号和入锁年度
        if (!req.body.companies || !req.body.company_year ) {
            $.sendMessage(res, 1015, '缺少参数 companies or company_year ')
            return
        }

        var check_is_year_exist = await Year.findOne({
            where: {
                id: req.body.company_year,
                type: '1'
            }
        }) 

        if (check_is_year_exist == null) {
            $.sendMessage(res, 1015, '请检查该年度是否存在')
            return
        }

        req.body.companies.map((item, index) => {
            req.body.companies[index]['company_year'] = req.body.company_year
        })

        delete req.body.id
        
        var insert = await Company.bulkCreate(req.body.companies)
        

        res.json({
            code: 200,
            message: '添加岗位至年度成功',
            result: insert
        })

        // 日志
        utils.create_a_log(req, '年度岗位', '添加岗位')
    }
    catch(error) {
        $.log('error :', error)
        res.json({
            code: 0000,
            message: '抓到异常',
            error: error
        })
        utils.create_a_log(req, '年度岗位', '添加岗位', '失败')
    }
}
    
module.exports = add_jobs